home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!news
- From: mjudge@ix.netcom.com(Michael Judge )
- Newsgroups: comp.lang.c++
- Subject: Re: question assignment operator in c++
- Date: 9 Mar 1996 17:23:04 GMT
- Organization: Netcom
- Message-ID: <4hsepo$kpu@dfw-ixnews4.ix.netcom.com>
- References: <menghua_wang.1.002DB7BE@muccmail.missouri.edu>
- NNTP-Posting-Host: bos-ma9-01.ix.netcom.com
- X-NETCOM-Date: Sat Mar 09 11:23:04 AM CST 1996
-
- In <menghua_wang.1.002DB7BE@muccmail.missouri.edu>
- menghua_wang@muccmail.missouri.edu (menghua wang) writes:
- >
- >I do not why the assignment operator can only be overloaded as a
- member
- >function in a class such as:
- >
- > class myclass {
- > int i;
- > public:
- > myclass operator= (myclass &A) {
- > i = A.i;
- > return *this;
- > }
- > };
- >
- >Why does not the following frind overloading work?
- >
- > mycalss operator= (myclass &A, myclass &B) {
- > A.i = B.i;
- > return A;
- > }
- >
- >thank you for your help
- >
-
- Ok. your problem is that you are not returning a reference to myclass.
- In the first example, you have added an operator = to the class and so
- allowing a copy constructor to be made. the friend function is trying
- to make a new myclass to return and is failing to find the
- myclass::operator= since you took it out. change the second to
-
- myclass &operator=(myclass &A,myclass &B){
- A.i=B.i;
- return A
- }
-
- that should be fine. If not, sue me.
- Michael Judge
- mjudge@ix.netcom.com
-
-